package run.graphics;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import log.LogConstants;

import run.ScenarioManager;

public class ExpeScreen extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6908449821860947990L;
	
	///////////////////////////////////////////////////////////////////////////
	
	private ScenarioManager manager;
	private JLabel trialData = new JLabel();
	private JTextArea textArea = new JTextArea();
	
	public ExpeScreen(final ScenarioManager manager, int fontSize) {
		
		super();
		
		this.manager = manager;
		
		this.setLayout(new BorderLayout());
		
		trialData.setHorizontalAlignment(SwingConstants.CENTER);
		trialData.setPreferredSize(new Dimension(500, 50));
		add(trialData, BorderLayout.NORTH);
		
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		textArea.setSelectionColor(Color.white);
		textArea.setFont(new Font(null, Font.PLAIN, fontSize));
		
		add(textArea, BorderLayout.CENTER);
		
		textArea.setVisible(true);
		
		textArea.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				manager.pressed();
			}
		});
		
		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent evt) {
				super.keyTyped(evt);
				if (evt.getKeyChar() == ' ') {
					manager.setCanStart(true);
				}
			}
		});
		
//		((Container) getGlassPane()).setLayout(new BoxLayout((Container) getGlassPane(), BoxLayout.PAGE_AXIS));
		JLabel pressSpaceLabel = new JLabel(manager.getWord("PressSpace"));
		pressSpaceLabel.setFont(new Font(null, Font.BOLD, 40));
		pressSpaceLabel.setBackground(Color.white);
		pressSpaceLabel.setOpaque(true);
		pressSpaceLabel.setBorder(BorderFactory.createLineBorder(Color.red, 5));
		((Container) getGlassPane()).setLayout(new GridBagLayout());
		((Container) getGlassPane()).add(pressSpaceLabel);
		getGlassPane().setVisible(true);
		
		setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
//		setResizable(true);
		
		// setBackground(Color.white);
		
	    GraphicsDevice gd =
	            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

	    if (gd.isFullScreenSupported()) {
	        setUndecorated(true);
	        gd.setFullScreenWindow(this);
	    } else {
	        System.err.println("Full screen not supported");
	        setSize(100, 100); // just something to let you see the window
	        setVisible(true);
	    }
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		
	}
	
	public void newTrial() {
		
		trialData.setText(manager.getWord(manager.getTask()) + " - " + manager.getWord(manager.getHand()) + " (" + manager.getLanguage() + ")");
		
		if (manager.getTask().equals(LogConstants.Reading)) {
			textArea.setText(manager.getReadingText());
		} else {
			textArea.setText("");
		}
		
		getGlassPane().setVisible(true);
		
//		repaint();
	}
	
	public void readyToTap() {
		getGlassPane().setVisible(false);
	}
	
}
